package cn.edu.scau.cmil.shishuai.comprehensive.address.view;

import java.io.File;

import cn.edu.scau.cmil.shishuai.comprehensive.address.MainApp;
import cn.edu.scau.cmil.shishuai.comprehensive.address.model.Person;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.chart.Axis;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.stage.Stage;

public class MainController {
	
	MainApp MainApp = new MainApp(); 

    private ObservableList<Person> StudentData = FXCollections.observableArrayList();

    public ObservableList<Person> getStudentData() {
        return StudentData;
    }
    public void addStudentData(Person person) {
    	this.StudentData.add(person);
    }
	
	@FXML
	private Label name;
	@FXML
	private Label lession;
	@FXML
	private TableView<Person> tableView;
	@FXML
	private TableColumn<Person, String> tc_user;
	@FXML
	private TableColumn<Person, String> tc_name;
	@FXML
	private TableColumn<Person, String> tc_s1;
	@FXML
	private TableColumn<Person, String> tc_s2;
	@FXML
	private TableColumn<Person, String> tc_s3;
	@FXML
	private TableColumn<Person, String> tc_s4;
	@FXML
	private TableColumn<Person, String> tc_s5;
	@FXML
	private Button save;
	@FXML
	private Button exit;
	@FXML
	private MenuButton profess;
	@FXML
	private MenuButton classroom;
	@FXML
	private PieChart pie = new PieChart();
	@FXML
	private BarChart<String, Number> bar  = new BarChart<>(new CategoryAxis(), new NumberAxis());
;
	@FXML
	public void initialize() {
		// TODO Auto-generated method stub
		File personFile = MainApp.getPersonFilePath();
        if (personFile != null) {
        MainApp.loadPersonDataFromFile(personFile);
        } 
        
        for(int i = 0;i<MainApp.getPersonData().size();i++) {
			if(MainApp.getPersonData().get(i).getLabel().equals("S"))
				StudentData.add(MainApp.getPersonData().get(i));
		}
        
		piechart();
		barchart();
        
        profess.getItems().clear();
        classroom.getItems().clear();
        profess.getItems().addAll(new MenuItem("全部"));
        classroom.getItems().addAll(new MenuItem("全部"));
        for(int i=0;i<StudentData.size();i++) {
        	String item;
        	int b = 1;

        	for(int j=i-1;j>=0;j--) {
        		
        	if(i == 0) {
        		item  = StudentData.get(i).getProfess();
        		profess.getItems().addAll(new MenuItem(item));
        		
        	}else if(StudentData.get(i).getProfess().equals(StudentData.get(j).getProfess())) {
        		b = 0;
        	}
        	}
        	if(b==1) {
        		item  = StudentData.get(i).getProfess();
        		profess.getItems().addAll(new MenuItem(item));
        	}
        	
        }
        for(int i=0;i<StudentData.size();i++) {
        	String item;
        	int b = 1;

        	for(int j=i-1;j>=0;j--) {
        		
        	if(i == 0) {
        		item  = StudentData.get(i).getClassroom();
        		classroom.getItems().addAll(new MenuItem(item));
        		
        	}else if(StudentData.get(i).getClassroom().equals(StudentData.get(j).getClassroom())) {
        		b = 0;
        	}
        	}
        	if(b==1) {
        		item  = StudentData.get(i).getClassroom();
        		classroom.getItems().addAll(new MenuItem(item));
        	}
        	
        }
        
        
        LoginController user = new LoginController();
        name.setText(user.userperson.getName());
		tc_user.setCellValueFactory(cellData -> cellData.getValue().UserProperty());
		tc_name.setCellValueFactory(cellData -> cellData.getValue().NameProperty());
		tc_s1.setCellValueFactory(cellData -> cellData.getValue().Score1Property());
		tc_s2.setCellValueFactory(cellData -> cellData.getValue().Score2Property());
		tc_s3.setCellValueFactory(cellData -> cellData.getValue().Score3Property());
		tc_s4.setCellValueFactory(cellData -> cellData.getValue().Score4Property());
		tc_s5.setCellValueFactory(cellData -> cellData.getValue().Score5Property());

		tableView.setEditable(true);
		tc_s1.setCellFactory(TextFieldTableCell.forTableColumn());
		tc_s2.setCellFactory(TextFieldTableCell.forTableColumn());
		tc_s3.setCellFactory(TextFieldTableCell.forTableColumn());
		tc_s4.setCellFactory(TextFieldTableCell.forTableColumn());
		tc_s5.setCellFactory(TextFieldTableCell.forTableColumn());

		
		tableView.setItems(StudentData);



	}
	@FXML
	public void cleanTable() {
		for(int i=0;i<StudentData.size();i++) {
			StudentData.get(i).setScore1(tc_s1.getCellData(i));
			StudentData.get(i).setScore2(tc_s2.getCellData(i));
			StudentData.get(i).setScore3(tc_s3.getCellData(i));
			StudentData.get(i).setScore4(tc_s4.getCellData(i));
			StudentData.get(i).setScore5(tc_s5.getCellData(i));
			tableView.refresh();
		}
		for(int i=0;i<StudentData.size();i++) {
			for(int j=0;j<MainApp.getPersonData().size();j++) {
				if(StudentData.get(i).getUser().equals(MainApp.getPersonData().get(j).getUser())) {
					MainApp.getPersonData().get(j).setScore1(StudentData.get(i).getScore1());
					MainApp.getPersonData().get(j).setScore2(StudentData.get(i).getScore2());
					MainApp.getPersonData().get(j).setScore3(StudentData.get(i).getScore3());
					MainApp.getPersonData().get(j).setScore4(StudentData.get(i).getScore4());
					MainApp.getPersonData().get(j).setScore5(StudentData.get(i).getScore5());
				}
					
			}
		}

		handleSave();
		
		
	}
	@FXML
	private void onMenuButtonClick() {
		for(int i=0;i<profess.getItems().size();i++) {
	//		if(profess.getItems().get(i).is)
	//		profess.getItems().get(i).
		}
		
	}
//	@FXML
//	private void setProjectLabel(ActionEvent event) { 
//	    MenuItem source = (MenuItem) event.getSource(); 
//	    profess.setText(source.getText()); 
//	    System.out.println(source.getText());
//	    profess.setText("1");
//	} 
	@FXML
    public void handleSave() {
        File personFile = MainApp.getPersonFilePath();
        if (personFile != null) {
            MainApp.savePersonDataToFile(personFile);
        } 
    }
	public void piechart() {
		int num1,num2,num3,num4,num5;
		num1=num2=num3=num4=num5=0;
		for(int i=0;i<StudentData.size();i++) {
			double score = Double.parseDouble(StudentData.get(i).getScore5().toString());
			if(score<=59) num1++;
			else {
				if(score<=69) num2++;
				else {
					if(score<=79) num3++;
					else {
						if(score<=89) num4++;
						else {
							if(score<=100)
								num5++;
						}
					}
				}
			}
		}
		
		ObservableList<PieChart.Data> piedataList  = FXCollections.observableArrayList();
		piedataList.addAll(
                new PieChart.Data("0~59"+"("+num1+"名)", num1),
                new PieChart.Data("60~69"+"("+num2+"名)", num2),
                new PieChart.Data("70~79"+"("+num3+"名)", num3),
                new PieChart.Data("80~89"+"("+num4+"名)", num4),
                new PieChart.Data("90~100"+"("+num5+"名)", num5));

		pie.setData(piedataList);
		pie.setTitle("学生成绩分布");
		pie.setVisible(true);	
 
	}
	public void barchart() {

		ObservableList<Series<String, Number>> barList  = FXCollections.observableArrayList();
		Series s = new Series();
		
		for(int i=0;i<StudentData.size();i++) {
			s.getData().add(new XYChart.Data(StudentData.get(i).getName(), Double.parseDouble(StudentData.get(i).getScore5().toString())));
		}
		barList.add(s);
        bar.setTitle("学生成绩排列");
        bar.getXAxis().setLabel("姓名");
        bar.getYAxis().setLabel("分数");
        bar.setData(barList);

	}

	@FXML
	public void onExit() {
		Stage stage = (Stage)exit.getScene().getWindow();
	    stage.close();
	}

}
